package com.atguigu.async;

import com.atguigu.exception.SleepUtils;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class FutureDemo02 {
    public static void main(String[] args) throws Exception {
        runAsync();
        System.out.println("main线程在执行");
    }

    //发起一个异步请求
    /**
     * 你好runAsync
     * 获取执行完异步方法之后的值:null
     * 获取执行完异步方法的异常:java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
     * 获取执行完异步方法的异常:java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
     * main线程在执行
     */
    public static void runAsync() {
        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println("你好runAsync");
                int a = 10 / 0;
            }
        }).whenComplete(new BiConsumer<Void, Throwable>() {
            @Override
            public void accept(Void acceptVal, Throwable throwable) {
                System.out.println("获取执行完异步方法之后的值:" + acceptVal);
                System.out.println("获取执行完异步方法的异常:" + throwable);
            }
        }).exceptionally(new Function<Throwable, Void>() {
            @Override
            public Void apply(Throwable throwable) {
                System.out.println("获取执行完异步方法的异常:" + throwable);
                return null;
            }
        });
    }

}
